﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using BLL;
using DAL;
using System.Text;

public partial class Public_Default : System.Web.UI.Page
{
    String StrConn =WebConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
    LoginHistoryHandler objLoginHistoryHandler = new LoginHistoryHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        UsersHandler objUsersHandler = new UsersHandler();
        List<Users> lstUsers =  objUsersHandler.GetUsersList(StrConn);
        Users objUsers = new Users();

        string idUser = id.Text.ToString();
        string passUser = password.Text.ToString();

        foreach(Users obj in lstUsers) {
            string iid = obj.UserName.ToString();
            string pass = obj.Password.ToString();

            if (idUser.Equals(iid) && passUser.Equals(pass))
            {
                LoginHistory objLoginHistory = new LoginHistory();

                string UserId = obj.UserID.ToString();
                Session["UserName"] = obj.UserName.ToString();
                Session["UserId"] = UserId;
                string SessionID = Session.SessionID.ToString();
                Session["SessionID"] = SessionID;

                objLoginHistory.UserID = Convert.ToInt32(UserId);
                objLoginHistory.LoginDate = DateTime.Now.ToString();
                Session["LoginDate"] = objLoginHistory.LoginDate;
                objLoginHistory.LogoutDate = DateTime.Now.ToString();
                objLoginHistory.Descriptions = "";
                objLoginHistory.SessionId = SessionID;

                objLoginHistoryHandler.AddNewLoginHistory(objLoginHistory, StrConn);



                Response.Redirect("Home.aspx");
            }
            //else
            //{
            //    string messagebox = "Error, Please try again";
            //    StringBuilder stringbuilder = new StringBuilder();
            //    stringbuilder.Append("<script language='javascript'>");
            //    stringbuilder.Append("alert(\"" + messagebox + "\");");
            //    stringbuilder.Append("<" + "/script>");
            //    Response.Write(stringbuilder.ToString());
            //    break;
            //}
            
        }

        //string ten = objUsers.UserID.ToString();
        

        

        /*
        imgCaptcha.ImageUrl = "~/CreateCaptcha.aspx?New=0";
        if (Session["CaptchaCode"] != null && captcha.Text == Session["CaptchaCode"].ToString())
        {
            //lblMessage.ForeColor = Color.Green;
            //lblMessage.Text = "Captcha code validated successfully!!";
        }
        else
        {
            //lblMessage.ForeColor = Color.Red;
            //lblMessage.Text = "Captcha code is wrong!!";
        }
         * */
        
    }
}